<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Document level security | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'document-level-security.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/document-level-security.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/document-level-security.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/document-level-security.html" rel="nofollow" target="_blank">../en/document-level-security.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="secure-cluster.html">Secure a cluster</a></span>
»
<span class="breadcrumb-link"><a href="authorization.html">User authorization</a></span>
»
<span class="breadcrumb-node">Document level security</span>
</div>
<div class="navheader">
<span class="prev">
<a href="security-privileges.html">« Security privileges</a>
</span>
<span class="next">
<a href="field-level-security.html">Field level security »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="document-level-security"></a>Document level security<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/security/authorization/document-level-security.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>Document level security restricts the documents that users have read access to.
In particular, it restricts which documents can be accessed from document-based
read APIs.</p>
<p>To enable document level security, you use a query to specify the documents that
each role can access. The document query is associated with a particular index
or index pattern and operates in conjunction with the privileges specified for
the indices.</p>
<p>The following role definition grants read access only to documents that
belong to the <code class="literal">click</code> category within all the <code class="literal">events-*</code> indices:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_security/role/click_role
{
  "indices": [
    {
      "names": [ "events-*" ],
      "privileges": [ "read" ],
      "query": "{\"match\": {\"category\": \"click\"}}"
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1243.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>Omitting the <code class="literal">query</code> entry entirely disables document level security for
      the respective indices permission entry.</p>
</div>
</div>
<p>The specified <code class="literal">query</code> expects the same format as if it was defined in the
search request and supports the full Elasticsearch <a href="query-dsl.html" class="ulink" target="_top">Query DSL</a>.</p>
<p>For example, the following role grants read access only to the documents whose
<code class="literal">department_id</code> equals <code class="literal">12</code>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_security/role/dept_role
{
  "indices" : [
    {
      "names" : [ "*" ],
      "privileges" : [ "read" ],
      "query" : {
        "term" : { "department_id" : 12 }
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1244.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">query</code> also accepts queries written as string values.</p>
</div>
</div>
<p>For more information, see <a class="xref" href="field-and-document-access-control.html" title="Setting up field and document level security">Setting up field and document level security</a>.</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="security-privileges.html">« Security privileges</a>
</span>
<span class="next">
<a href="field-level-security.html">Field level security »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>